package com.cloud.service.user.impl;

import com.cloud.bean.user.User;
import com.cloud.common.BaseReturn;
import com.cloud.controller.login.from.FromUser;
import com.cloud.mapper.user.UserMapper;
import com.cloud.service.user.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liuyuan
 * @since 2022-07-21
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    UserMapper userMapper;
    @Override
    public BaseReturn login(FromUser fromUser) {
        String mobile = fromUser.getMobile();
        String password = fromUser.getPassword();
        HashMap<String, Object> queryMap = new HashMap<>();
        queryMap.put("mobile",mobile);
        queryMap.put("password",password);
        //通过手机号和密码查询表判断登录
        List<User> users = userMapper.selectByMap(queryMap);
        //用户能查出来一个才能登录成功
        if( users.size() == 1){
            //传过来的登录平台
            String loginType = fromUser.getLoginType();
            User user = users.get(0);
            //判断是在管理端登录还是在用户端登录
            if(loginType.equals("ADMIN")){
                String userRole = user.getUserRole();
                //判断用户角色有没有权限进入
                if(userRole.equals("STAFF") || userRole.equals("ADMIN")){
                    return BaseReturn.build("2000","登录成功",user);
                }else {
                    return BaseReturn.build("5002","你没有此权限进入");
                }
            }else {
                return BaseReturn.build("2000","登录成功",user);
            }
        }else {
            return BaseReturn.build("5002","账号或者密码不存在");
        }
    }
}
